package cn.psh.pet.service.impl;

import cn.psh.basic.service.IBaiduAiAuditService;
import cn.psh.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.psh.basic.util.AjaxResult;
import cn.psh.basic.util.PageList;
import cn.psh.pet.domain.Pet;
import cn.psh.pet.domain.PetDetail;
import cn.psh.pet.domain.PetOnlineAuditLog;
import cn.psh.pet.mapper.PetDetailMapper;
import cn.psh.pet.mapper.PetMapper;
import cn.psh.pet.mapper.PetOnlineAuditLogMapper;
import cn.psh.pet.query.PetQuery;
import cn.psh.pet.service.IPetService;
import cn.psh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pider
 * @since 2022-09-23
 */
@Transactional
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;


    @Transactional
    @Override
    public void save(Pet pet) {
        super.save(pet);
//        获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }

    }
    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delPetDetailById(id);
    }
    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        final PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }
    @Transactional
    @Override
    public void batchDel(List<Long> ids) {
        super.batchDel(ids);
        for (Long id : ids) {
            del(id);
        }
    }
    @Transactional
    @Override
    public AjaxResult onsale(List<Long> ids) {
//      上架审核【内容审核，图形审核】
        for (Long id : ids) {
            Pet pet = petMapper.queryById(id);
//            审核内容和图像
            Boolean aBoolean = baiduAiAuditService.textAudit(pet.getName());
            Boolean aBoolean1 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            if(aBoolean && aBoolean1){
//                该状态！
                petMapper.onsale(id,new Date());
//                添加审核日志
                log.setState(1);
                log.setNote("审核通过");
                petOnlineAuditLogMapper.save(log);
            }else {
//                审核失败
                log.setState(0);
                log.setNote("审核失败");
                log.setAuditTime(new Date());
                petOnlineAuditLogMapper.save(log);
                return new AjaxResult().setSuccess(false).setMessage("图片审核不通过");
            }
        }
        return AjaxResult.OJBK().setSuccess(true);
    }
    @Transactional
    @Override
    public AjaxResult offsale(List<Long> ids) {
//      下架，改状态，加时间，
        petMapper.offsale(ids,new Date());
        return new AjaxResult();
    }

    @Override
    public List<Pet> queryShopAll(Long shopId) {
        PetQuery petQuery = new PetQuery();
        petQuery.setShopId(shopId);
        List<Pet> pets = petMapper.queryData(petQuery);
        return pets;
    }
}
